# main.py（增强版）

import os
import sys
import subprocess
import threading
import time

SCRIPTS = {
    "User Input GUI": "user_input_gui.py",
    "Scheduler": "scheduler.py",
    "Visualizer": "visualize.py"
}

def run_script(script_name, script_file):
    """在独立线程中运行指定的 Python 脚本"""
    def worker():
        print(f" 正在启动: {script_name}")
        try:
            process = subprocess.Popen([sys.executable, script_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)

            # 实时输出日志
            for line in iter(process.stdout.readline, ''):
                print(f"[{script_name}] {line.strip()}")

            process.stdout.close()
            process.wait()
            print(f" {script_name} 已关闭")
        except Exception as e:
            print(f" 启动 {script_name} 出错: {e}")

    thread = threading.Thread(target=worker, daemon=True)
    thread.start()
    return thread

def main():
    print("=" * 60)
    print("欢迎使用一体化数据处理平台".center(40))
    print("=" * 60)
    print("\n[INFO] 即将启动以下模块：")
    for name, file in SCRIPTS.items():
        print(f" - {name}: {file}")
    print()

    if not os.path.exists("input"):
        os.makedirs("input")
        print(" 创建目录: input")

    threads = {}
    for name, file in SCRIPTS.items():
        if os.path.exists(file):
            print(f" 正在尝试启动: {name}...")
            threads[name] = run_script(name, file)
        else:
            print(f" 文件不存在，跳过启动: {file}")

    print("\n 所有模块已启动，主程序将持续运行...")
    print(" GUI 应该已经弹出，请查看是否有提示或窗口出现。\n")

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("\n 用户中断，正在退出...")

if __name__ == "__main__":
    main()